<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>顾客管理 iframe 框</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
    <style type="text/css">
        .mask {
            position: absolute;
            top: 0px;
            filter: alpha(opacity=60);
            background-color: white;
            z-index: 100000;
            left: 0px;
            opacity: 0.01;
            -moz-opacity: 0.01;
            display: none;
        }
    </style>
</head>
<body>
<div id="mask" class="mask"></div>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list"
     style="padding: 20px 30px 0 0;">
    <input type="hidden" id="employeeId"/>
    <div class="layui-form-item">
        <label class="layui-form-label">真实名称</label>
        <div class="layui-input-inline">
            <input type="text" id="realName" lay-verify="required" placeholder="请输入真实名称" autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话号码</label>
        <div class="layui-input-inline">
            <input type="text" id="employeePhone" onblur="checkPhone()" lay-verify="required|phone"
                   placeholder="请输入电话号码"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号</label>
        <div class="layui-input-inline">
            <input type="text" id="employeeIDNumber" lay-verify="required|identity" placeholder="请输入身份证号"
                   autocomplete="off"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="employeeSex" value="0" title="男" checked>
            <input type="radio" name="employeeSex" value="1" title="女">
        </div>
    </div>
    <!--    <div class="layui-form-item">-->
    <!--        <label class="layui-form-label">生日</label>-->
    <!--        <div class="layui-input-block">-->
    <!--            <input type="text" class="layui-input" id="employeeBirthday" placeholder="yyyy-MM-dd">-->
    <!--        </div>-->
    <!--    </div>-->
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="employee-submit" id="employee-submit"
               value="确认添加">
        <input type="button" lay-submit lay-filter="employee-edit" id="employee-edit"
               value="确认编辑">
    </div>
</div>

<script src="../../layuiadmin/layui/layui.js"></script>
<script src="../../js/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
    //显示遮罩层
    function showMask() {
        $("#mask").css("height", $(document).height());
        $("#mask").css("width", $(document).width());
        $("#mask").show();
    }

    //隐藏遮罩层
    function hideMask() {
        $("#mask").hide();
    }

    //格式化日期
    function dateFormat(value) {
        if (null != value && "" != value) {
            var date = new Date(value);
            var y = date.getFullYear();
            var m = date.getMonth() + 1;
            if (m < 10) {
                m = "0" + m;
            }
            var d = date.getDate();
            if (d < 10) {
                d = "0" + d;
            }
            return y + '-' + m + '-' + d;
        } else {
            return "";
        }
    }
</script>
<script>
    // 检查号码有没有被使用
    function checkPhone() {
        if ($("#employeePhone").val() != "") {
            //下拉框初始化
            $.ajax({
                url: parent.findPhoneURL,
                type: 'get',
                dataType: 'json',
                data: {"phone": $("#employeePhone").val()},
                async: false,//设置阻断
                success:
                    function (data) {
                        if (data.status == 200) {
                        } else {
                            layer.msg(data.msg);
                            $("#employeePhone").val("");
                        }
                    }
            })
            ;
        }
    }

    //layui配置
    layui.config({
        base: '../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function () {
        var $ = layui.$
            , form = layui.form
            , laydate = layui.laydate;

        //监听提交
        form.on('submit(employee-submit)', function (data) {
            var field = data.field; //获取提交的字段
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            //获取id，若无则改为0
            var id = $("#employeeId").val();
            if (id == null || id == "") id = 0;
            //提交 Ajax 成功后，关闭当前弹层并重载表格
            $.ajax({
                url: parent.employeeURL,
                type: 'post',
                dataType: 'json',
                data: {
                    "id": id,
                    "realname": $("#realName").val(),
                    "phone": $("#employeePhone").val(),
                    "sex": $("input[type=radio][name=employeeSex]:checked").val(),
                    "birthday": "",
                    "idnumber": $("#employeeIDNumber").val(),
                },
                success: function (data) {
                    parent.layer.msg(data.msg, {icon: data.icon, zIndex: layer.zIndex + 100});
                }
            });
            parent.layui.table.reload('employeeTable'); //重载表格
            parent.layer.close(index); //再执行关闭
        });
    })

    //根据父级页面判断，如果是修改，则添加默认值
    if (parent.employeeType == "toUpdate") {
        $.ajax({
            url: parent.toUpdateURL,
            type: 'post',
            dataType: 'json',
            async: false,//设置阻断
            data: {"id": parent.employeeId},
            success: function (data) {

                //修改性别radio
                if (data.data.sex == 1) {
                    $("input[name=employeeSex][value='1']").prop("checked", "true");
                }
                //重新渲染表单元素
                layui.use('form', function () {
                    var form = layui.form;
                    form.render();
                });
                $("#employeeId").val(parent.employeeId);
                $("#realName").val(data.data.realname);
                $("#employeePhone").val(data.data.phone);
                var birthday = data.data.birthday == null ? "暂无信息" : data.data.birthday;
                if (birthday != "暂无信息") {
                    birthday = dateFormat(birthday);
                }
                // $("#employeeBirthday").val(birthday);
                $("#employeeIDNumber").val(data.data.idnumber);

            }
        });
        if (parent.employeeURL == "detail") {
            showMask();
        }
    }
</script>
</body>
</html>